package com.bite.springbook.controller;


import com.bite.springbook.constant.Constants;
import com.bite.springbook.enums.StatusEnum;
import com.bite.springbook.model.*;
import com.bite.springbook.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(RequestPage requestPage , HttpSession session){
        log.info("requestPage接收到的参数 : {} ," , requestPage);
        //参数校验，用户是否登入.
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.USER_SESSION_KEY);
        if(userInfo == null){
            return Result.nologin();
        }
        PageResult<BookInfo> result = bookService.getLsitByPage(requestPage);
        return Result.success(result);
    }

    @RequestMapping("/addBook")
    public boolean addBook(BookInfo bookInfo , HttpSession session){
        log.info("BookController.addBook 接收参数: {}", bookInfo);
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.USER_SESSION_KEY);
        if(userInfo == null){
            return true;
        }
        //1.参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null){
            log.warn("参数不正确");
            return false;
        }
        //2.操作数据库
        Integer result = bookService.addBook(bookInfo);
        if (result==0){
            log.error("添加图书失败");
            return false;
        }
        return true;
    }

    @RequestMapping("/getBookInfo")
    public BookInfo getBookInfo(Integer bookId){
        log.info("getBookInfo, book : {}", bookId);
        BookInfo bookInfo = bookService.getBookInfo(bookId);

        return bookInfo;
    }

    @RequestMapping("/updateBook")
    public boolean UpdateBook(BookInfo bookInfo , HttpSession session){
        log.info("update, bookInfo : {}" , bookInfo);
        if(bookInfo.getId() == null){
            log.warn("Id不能为空");
            return false;
        }
        if(bookInfo.getBookName() != null && "".equals(bookInfo.getBookName().trim())){
            log.warn("图书名称不能为空");
            return false;
        }

        try{
            Integer result = bookService.updateBookIfo(bookInfo);
            if(result > 0){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("update error , e : {}", e);
            return false;
        }
    }

    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer bookId){
        try{
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(StatusEnum.DELETED.getCode());
            Integer result = bookService.updateBookIfo(bookInfo);
            if(result > 0){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("update error , e : {}", e);
            return false;
        }
    }

    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("batchDelete ides :{}" , ids);
        if(ids == null || ids.size() == 0){
            return true;
        }
        try{
            Integer result = bookService.batchDelete(ids);
            if(result > 0){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("batchDelete error" , e);
            return false;
        }
    }

}
